//
//  ProductDetailHeaderView.swift
//  产品详情 - itme View
//
//  Created by 千策万划-mini on 2025/5/2.
//

import Foundation

class ProductDetailHeaderView: UIView {
    
    var modelList: [ModelProductDetailTellingInfo] = []
    var blockAuthItemClick:((ModelProductDetailTellingInfo) -> Void)?
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, 0)
        self.backgroundColor = .clear
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        if let block = blockAuthItemClick {
            block(modelList[sender.tag])
        }
    }
    
    // MARK: - 重置界面
    func resetView(list: [ModelProductDetailTellingInfo] = []) {
        modelList = list
        let viewWidth = W(154)
        let leftMargin = (SCREEN_WIDTH-viewWidth*2)/3
        if isAry(list) {
            self.removeAllSubviews()
            for i in 0..<list.count {
                let model = list[i]
                let viewBack = UIButton()
                viewBack.isUserInteractionEnabled = true
                viewBack.widthHeight = XY(viewWidth, 0)
                viewBack.backgroundColor = .clear
                
                let image = UIImageView()
                // "darker": 1, //是否已完成认证 0否1是，
                var imageName = "icon_verify_auth"
                if model.died == "Chatterer" {
                    // 身份证
                    if model.darker == 1 {
                        imageName = "icon_auth"
                    }else{
                        imageName = "icon_noauth"
                    }
                }else if model.died == "Forest" {
                    // 个人信息
                    if model.darker == 1 {
                        imageName = "icon_auth"
                    }else{
                        imageName = "icon_noauth"
                    }
                }else if model.died == "Shadow" {
                    // 工作
                    if model.darker == 1 {
                        imageName = "icon_auth"
                    }else{
                        imageName = "icon_noauth"
                    }
                }else if model.died == "minute" {
                    // 联系人
                    if model.darker == 1 {
                        imageName = "icon_auth"
                    }else{
                        imageName = "icon_noauth"
                    }
                }else if model.died == "peeked" {
                    // 银行卡
                    if model.darker == 1 {
                        imageName = "icon_auth"
                    }else{
                        imageName = "icon_noauth"
                    }
                }
                image.image = UIImage(named: imageName)
                image.widthHeight = XY(viewWidth, W(139))
                image.leftTop = XY(0, 0)
                viewBack.addSubview(image)
                
                let imageLogo = UIImageView()
                imageLogo.widthHeight = XY(W(74), W(64))
                imageLogo.centerXTop = XY(image.width/2, W(25))
                imageLogo.backgroundColor = .clear
                
                imageLogo.setImage(urlStr: model.livesdfs)
                image.addSubview(imageLogo)
                
                
                let labelTitle = OutlineLabel()
                labelTitle.resetView(
                    title: model.whose,
                    textColor: COLOR_WHITE(),
                    font: UIFont.customBoldFont(size: 14),
                    shadowColor: UIColor(hex: "#2D0084"),
                    outlineColor: UIColor(hex: "#2D0084")
                )
                labelTitle.centerXBottom = XY(image.width/2, image.height-W(10))
                image.addSubview(labelTitle)
                
                let imageState = UIImageView()
                imageState.image = UIImage(named: model.darker == 1 ? "icon_product_detail_verify" : "icon_product_detail_not_verify")
                imageState.widthHeight = XY(viewWidth, W(28))
                imageState.leftTop = XY(0, image.bottom+W(8))
                viewBack.addSubview(imageState)
                
                let labelState = OutlineLabel()
                labelState.resetView(
                    title: model.darker == 1 ? "100%" : "Not Verified",
                    textColor: COLOR_WHITE(),
                    font: UIFont.customSemiBoldFont(size: 12),
                    shadowColor: UIColor(hex: "#2D0084"),
                    outlineColor: UIColor(hex: "#2D0084")
                )
                labelState.centerXCenterY = XY(imageState.width/2, imageState.height/2)
                imageState.addSubview(labelState)
                
                viewBack.height = imageState.bottom+W(24)
                viewBack.leftTop = XY(leftMargin+(viewWidth+leftMargin)*CGFloat(i%2), viewBack.height*CGFloat(i/2))
                
                viewBack.tag = i
                viewBack.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
                
                addSubview(viewBack)
                
                height = viewBack.bottom
            }
        }
    }
}
